package com.unionpay.upyzt.example;

import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.TransferRefund;
import com.unionpay.upyzt.resp.TransferRefundResp;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 转账入金退款
 */
public class TransferRefundExample {

    public static void runDemos() throws UpyztException {
        TransferRefundExample example = new TransferRefundExample();
        // 退款
        TransferRefundResp refundResp = example.refunds();
        // 退款订单查询（系统订单号）
        refundResp = example.retrieveById(refundResp.getTransferRefundId());
        // 退款订单查询（平台订单号）
        refundResp = example.retrieveByOutOrderNo(refundResp.getOutOrderNo());
    }

    // 转账入金退款
    private TransferRefundResp refunds() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("amount", 100);
        params.put("orig_trade_id", "");
        params.put("remark", "用于 SDK 示例测试");
        params.put("extra", MockData.extra());
        params.put("metadata", MockData.metadata());
        System.out.println("------- 转账入金退款  -------");
        TransferRefundResp resp = TransferRefund.refunds(params);
        System.out.println(resp);
        return resp;
    }

    // 退款订单查询（系统订单号）
    private TransferRefundResp retrieveById(String transferRefundId) throws UpyztException {
        System.out.println("------- 退款订单查询（系统订单号）  -------");
        TransferRefundResp resp = TransferRefund.retrieveById(transferRefundId);
        System.out.println(resp);
        return resp;
    }

    // 退款订单查询（平台订单号）
    private TransferRefundResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 退款订单查询（平台订单号）  -------");
        TransferRefundResp resp = TransferRefund.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

}
